package com.example.myapplication;


import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;



public class ChartCircleWaterView extends View {
    private Paint paint;
    private RectF rectF;

    //绘画的进度
    private float percent = 1.0f;

    private PropertyPairsBean item;

    //动画持续时间
    private final int cvAnimDuration;
    private final int circleWidth = 8;
    private ValueAnimator anim;


    public void setItem(PropertyPairsBean item) {
        this.item = item;
        startAnim();
    }

    public ChartCircleWaterView(Context context) {
        this(context, null);
    }

    public ChartCircleWaterView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChartCircleWaterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ChartCircleView);
        cvAnimDuration = typedArray.getInt(R.styleable.ChartCircleView_cv_animDuration, 1500);
        //圆环比率，可以控制圆环的粗细
        float circleRate = typedArray.getFloat(R.styleable.ChartCircleView_cv_rate, 0.68f);
        if (circleRate > 0.9f) {
            circleRate = 0.9f;
        } else if (circleRate < 0) {
            circleRate = 0;
        }
        typedArray.recycle();
        initPaint();
    }

    private void initPaint() {
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(circleWidth);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setAntiAlias(true);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (changed) {
            int mWidth = getWidth();
            // 控件高度
            int mHeight = getHeight();
            int minLength = Math.min(mWidth, mHeight);
            // 圆环的半径
            int mRadius = minLength / 2 - circleWidth / 2;
            rectF = new RectF(getWidth() / 2 - mRadius, getHeight() / 2 - mRadius, getWidth() / 2 + mRadius, getHeight() / 2 + mRadius);
        }
    }


    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        // 绘制基础图背景图
        drawBaseBackground(canvas);
    }

    private void drawBaseBackground(Canvas canvas) {
        if (item == null) {
            paint.setColor(Color.parseColor("#272727"));
            canvas.drawArc(rectF, -90, 360, false, paint);
            return;
        }
        float currentAngle = item.getRatio() * 360;
        paint.setColor(Color.parseColor("#272727"));
        canvas.drawArc(rectF, -90, 360, false, paint);
        paint.setColor(ContextCompat.getColor(getContext(), item.getColor()));
        canvas.drawArc(rectF, -90, currentAngle * percent, false, paint);
    }

    private void startAnim() {
        if (anim == null)
            anim = ValueAnimator.ofFloat(0.0f, 1.0f);
        anim.setDuration(cvAnimDuration);
        anim.setInterpolator(new LinearInterpolator());
        anim.addUpdateListener(animation -> {
            percent = (float) animation.getAnimatedValue();
            invalidate();
        });
        if (!anim.isRunning())
            anim.start();
    }

}
